1
|
|
|
import { AngularFireAuth } from "@angular/fire/auth"; |
2
|
|
|
import { Platform } from "@ionic/angular"; |
3
|
|
|
import { auth } from "firebase"; |
4
|
|
|
import { UniFirebaseLoginConfig } from "../config/uni-firebase-login-config"; |
5
|
|
|
import { UniFirebaseLoginConfigProvider } from "../config/uni-firebase-login-config-provider"; |
6
|
|
|
import { IAuthProvider } from "./i-auth-provider"; |
7
|
|
|
|
8
|
|
|
export abstract class AbstractAuth implements IAuthProvider { |
9
|
|
|
public abstract readonly providerKey: |
10
|
|
|
| "anonymous" |
11
|
|
|
| "email" |
12
|
|
|
| "facebook" |
13
|
|
|
| "github" |
14
|
|
|
| "google" |
15
|
|
|
| "phone" |
16
|
|
|
| "twitter"; |
17
|
|
|
public readonly defaultOptions: any = {}; |
18
|
|
|
protected config: UniFirebaseLoginConfig; |
19
|
|
|
|
20
|
|
|
protected constructor( |
21
|
|
|
protected angularFireAuth: AngularFireAuth, |
22
|
|
|
protected platform: Platform, |
23
|
|
|
configProvider: UniFirebaseLoginConfigProvider, |
24
|
|
|
) { |
25
|
|
|
this.config = configProvider.config; |
26
|
|
|
this.applyDefaultOptions(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public abstract signInNative( |
30
|
|
|
options: any, |
31
|
|
|
): Promise<auth.UserCredential | null>; |
32
|
|
|
|
33
|
|
|
public async signIn(options: any = {}): Promise<auth.UserCredential | null> { |
34
|
|
|
if (this.platform.is("cordova")) { |
35
|
|
|
return this.signInNative(options); |
36
|
|
|
} else { |
37
|
|
|
return this.signInBrowser(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public async signOutNative(): Promise<void> { |
42
|
|
|
if (this.angularFireAuth.auth.currentUser === null) { |
43
|
|
|
console.warn("Unknown currentUser!"); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public async signOutBrowser(): Promise<void> { |
48
|
|
|
return this.angularFireAuth.auth.signOut(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public async signOut(): Promise<void> { |
52
|
|
|
if (this.platform.is("cordova")) { |
53
|
|
|
return this.signOutNative(); |
54
|
|
|
} else { |
55
|
|
|
return this.signOutBrowser(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public async signInBrowser(): Promise<auth.UserCredential | null> { |
60
|
|
|
const provider = this.getBrowserSignInProvider(); |
61
|
|
|
const authX = this.angularFireAuth.auth; |
62
|
|
|
|
63
|
|
|
switch (this.defaultOptions.signInType) { |
64
|
|
|
case "popup": |
65
|
|
|
return await authX.signInWithPopup(provider); |
66
|
|
|
case "redirect": |
67
|
|
|
await authX.signInWithRedirect(provider); |
68
|
|
|
// TODO: implement redirect resolution: https://stackoverflow.com/questions/40219478/firebaseauth-googleauthprovider-signinwithredirect |
69
|
|
|
throw new Error("Not implemented!"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
throw new Error("Invalid signInType!"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Apply default options to configuration for the provider |
77
|
|
|
*/ |
78
|
|
|
protected applyDefaultOptions(): any { |
79
|
|
|
if (this.config.providers[this.providerKey] === undefined) { |
80
|
|
|
this.config.providers[this.providerKey] = {}; |
81
|
|
|
} |
82
|
|
|
return Object.assign( |
83
|
|
|
this.config.providers[this.providerKey], |
84
|
|
|
this.defaultOptions, |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected abstract getBrowserSignInProvider(): any | null; |
89
|
|
|
} |
90
|
|
|
|